<template>
    <div class="login-box" ref="boxFlag">
        <div class="owl" ref="owl">
            <div class="hand"></div>
            <div class="hand hand-r"></div>
            <div class="arms">
                <div class="arm"></div>
                <div class="arm arm-r"></div>
            </div>
        </div>
        <div class="input-box">
            <input type="text" placeholder="账号：长度为8的数字或字母" v-model="userInfo.username">
            <input type="password" placeholder="密码：长度至少6位数字或字母" @keyup="handleKeyup" v-model="userInfo.password"
                @focus="addClass" @blur="removeClass">
            <button @click="submit">登录</button>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import type UserInfo from '@/types/UserInfo';
import { ElMessage } from 'element-plus';
import { useLoginStore } from '@/store/loginStore';

const loginStore = useLoginStore()
// 表单数据
const userInfo = reactive<UserInfo>({
    username: "",
    password: "",
    uid: "",
    key: ""
})
// 防止多次回车
const isEnterClicked = ref(true)

/**
 * 表单校验
 * 用户名不为空且由8位数字或字母组成
 * 密码不为空至少由6位数字或字母组成
 */
const validateForm = () => {
    if (
        userInfo.username.trim() == ""
        ||
        !/^[a-zA-Z0-9]{8}$/.test(userInfo.username)
        ||
        userInfo.password.trim() == ""
        ||
        !/^[A-Za-z0-9]{6,}$/.test(userInfo.password)
    ) {
        return false
    }
    return true
}

/**
 * 提交表单信息
 * 同时限制提交间隔：2s
 */
const submit = () => {
    // 防止用户多次点击
    if (isEnterClicked.value) {
        isEnterClicked.value = false
        // 2秒后可再次点击
        const timer = setTimeout(() => {
            isEnterClicked.value = true
            clearTimeout(timer)
        }, 2000)
        if (!validateForm()) {
            ElMessage.warning("格式错误，请仔细核对用户名和密码")
            clear()
            return
        }
        loginStore.login(userInfo)
    }
}

/**
 * 回车提交
 */
const handleKeyup = (e: KeyboardEvent) => {
    if (e.key == "Enter") {
        submit();
    }
}

/**
 * 清空用户输入的用户名和密码
 */
const clear = () => {
    userInfo.username = ""
    userInfo.password = ""
}

// 猫头鹰样式控制
const owl = ref();
function addClass() {
    owl.value.classList.add('password');
}
function removeClass() {
    owl.value.classList.remove('password');
}
</script>

<style scoped>
.login-box {
    /* 绝对定位 */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 320px;
}

.input-box {
    /* 弹性布局 垂直排列 */
    display: flex;
    flex-direction: column;
}

.input-box input {
    height: 40px;
    border-radius: 3px;
    /* 首行缩进 */
    text-indent: 15px;
    outline: none;
    border: none;
    margin-bottom: 15px;
}

.inoyt-box input:focus {
    outline: 1px solid lightseagreen;
}

.input-box button {
    border: none;
    height: 45px;
    background-color: lightseagreen;
    color: #fff;
    border-radius: 5px;
    cursor: pointer;
}

/* owl样式 */
.owl {
    width: 211px;
    height: 108px;
    background: url("@/assets/images/owl-login.png") no-repeat;
    /* 绝对定位 */
    position: absolute;
    top: -100px;
    /* 水平居中 */
    left: 50%;
    transform: translateX(-50%);

}

.owl .hand {
    width: 34px;
    height: 34px;
    border-radius: 40px;
    background-color: #472d20;
    /* 绝对定位 */
    position: absolute;
    left: 12px;
    bottom: -8px;
    /* 延y轴缩小 (压扁)*/
    transform: scaleY(0.6);
    /* 动画过渡 */
    transition: 0.3s ease-out;
}

.owl .hand.hand-r {
    left: 170px;
}

/* 双手移动 */
.owl.password .hand {
    transform: translateX(42px) translateY(-15px) scale(0.7);
}

.owl.password .hand.hand-r {
    transform: translateX(-42px) translateY(-15px) scale(0.7);
}

.owl .arms {
    position: absolute;
    top: 58px;
    width: 100%;
    height: 41px;
    overflow: hidden;
}

.owl .arms .arm {
    width: 40px;
    height: 65px;
    position: absolute;
    left: 20px;
    top: 40px;
    background: url("@/assets/images/owl-login-arm.png") no-repeat;
    transform: rotate(-20deg);
    transition: 0.3s ease-out;
}

.owl .arms .arm.arm-r {
    transform: rotate(20deg) scaleX(-1);
    left: 158px;
}

/* 移动 */
.owl.password .arms .arm {
    transform: translateY(-40px) translateX(40px);
}

.owl.password .arms .arm.arm-r {
    transform: translateY(-40px) translateX(-40px) scaleX(-1);
}
</style>